/*
 * @Author: szx
 * @Date: 2022-03-07 12:45:14
 * @LastEditTime: 2022-03-07 13:09:47
 * @Description:
 * @FilePath: \leetcode\1400-1499\1402\1402.js
 */
/**
 * @param {number[]} satisfaction
 * @return {number}
 */
var maxSatisfaction = function (satisfaction) {
    satisfaction.sort((a, b) => b - a);
    let res = 0;
    let presum = 0;
    for (let i of satisfaction) {
        if (presum + i >= 0) {
            presum += i;
            res += presum;
        } else break;
    }
    return res;
};

console.log(maxSatisfaction([-1, -5, 0, 5, -9]));
